home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 664 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  69 lines

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: bstowers@pobox.com (Brad Stowers)
  3. Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.std.c++
  4. Subject: Enumerated type converted to pointer? Legal?
  5. Date: 8 Mar 1996 04:04:02 -0000
  6. Organization: NETCOM Network Operations
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: harkness@airmail.net
  9. Message-ID: <4hobji$mco@netlab.cs.rpi.edu>
  10. Reply-To: bstowers@pobox.com
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12. X-Original-Date: Thu, 07 Mar 1996 21:10:37 GMT
  13.  
  14. #include <stdio.h>
  15.  
  16. typedef enum { mdType1, mdType2, mdType3 } myType;
  17.  
  18. void FooBar(int x,
  19.             int* y,
  20.             myType type = mdType1)
  21. {
  22.   printf("x=%i\ny=%p\ntype=%type\n", x, y, type);
  23. }
  24.  
  25. main()
  26. {
  27.   int x = 10;
  28.   int* y = &x;
  29.   FooBar(x, y, mdType2);
  30.   FooBar(x, mdType1);
  31. }
  32.  
  33. ------------------EOF------------------------------
  34.  
  35. Compile and run the above program.  Is it just me, or should the last line
  36. of the main() function ( FooBar(x, mdType1); ) have caused a type mismatch
  37. error?  I think I know why it didn't:  mdType1 evaluates to an integer with
  38. a value of 0, which is a valid value to pass as a pointer.  In support of
  39. this theory, the line:
  40.  
  41.   FooBar(x, mdType2);
  42.  
  43. will not compile, but rather generates the expected type mismatch error.
  44. Obviously, the work-around is simple:
  45.  
  46.   typedef enum { mdType1 = 1, mdType2, mdType3 } myType;
  47.  
  48. While this will solve the problem, I really think that it is the compiler's
  49. job to see that mdType1 does not evaluate to the proper type no matter what
  50. it's internal representation might be.
  51.  
  52. I have verified that this happens on both Borland and Symantec compilers.
  53. Can anyone verify it on Watcom and Microsoft for me?
  54.  
  55.  
  56. Regards,
  57. Brad
  58. --------------------------------------------------------
  59. Brad Stowers             |  bstowers@pobox.com
  60. deltaComm Development    |  Upward, but not Northward.
  61. Telix for Windows & DOS  |                 -- A Square
  62. --------------------------------------------------------
  63.            http://www.pobox.com/~bstowers/
  64.  
  65.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  66.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  67.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  68.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  69.